#include "init.h"
#include "externs.h"
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/resource.h>

void sigint_handler (int sig) {
    printf ("\n[minishell]$ ");
    fflush (stdout);
}

void setup (void) {
    signal (SIGINT, sigint_handler);
    signal (SIGQUIT, SIG_IGN);

    struct rlimit rl;
    if (0 == getrlimit (RLIMIT_NOFILE, &rl)) {
        fopen_cur = rl.rlim_cur;
        fopen_max = rl.rlim_max;
        // printf ("fopen_cur = %d\n", fopen_cur);
        // printf ("fopen_max = %d\n", fopen_max);
    } else {
        fopen_cur = 8192;
    }
}

void init (void){
    memset (cmdline, 0, sizeof (cmdline));
    memset (avline, 0, sizeof (avline));
    memset (cmd, 0, sizeof (cmd));
    for (int i = 0; i < PIPELINE; ++i) {
        cmd[i].infd = STDIN_FILENO; /* 标准输入文件描述符 */
        cmd[i].outfd = STDOUT_FILENO; /* 标准输出文件描述符 */
        cmd[i].errfd = STDERR_FILENO; /* 标准错误文件描述符 */
    }
    memset (infile, 0, sizeof (infile));
    memset (outfile, 0, sizeof (outfile));
    lineptr = cmdline;
    avptr = avline;
    cmd_count = 0;
    backgrnd = 0;
    append = 0;
    lastpid = 0;
    printf ("[minishell]$ ");
    fflush (stdout);//以上printf()中无'\n',
                    //不会自动将缓冲区中的字符清空而不显示打印内容,
                    //故需手动清空
}
